﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogOn.ascx.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the LogOn class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace LidocaineWeb.Modules
{
    using System;
    using System.Web;
    using System.Web.UI;
    using Lidocaine;

    /// <summary>
    /// Defines the LogOn class.
    /// </summary>
    public partial class LogOn : UserControl
    {
        /// <summary>
        /// This method is triggered on Page_Load.
        /// </summary>
        /// <param name="sender">The caller of the event.</param>
        /// <param name="e">The parameters of the event.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Environmental.UseSsl && !Request.IsSecureConnection)
            {
                Response.Redirect("https://" + Environmental.Domain + Request.RawUrl.ToString());
            }

            if (!this.IsPostBack)
            {
                if (Profile.Current != null)
                {
                    this.LogOnForm.Visible = false;
                    this.LogOffForm.Visible = true;
                }
            }
        }

        /// <summary>
        /// This method is triggered by clicking the Login button.
        /// </summary>
        /// <param name="sender">The caller of the event.</param>
        /// <param name="e">The parameters of the event.</param>
        protected void LogOn_Click(object sender, EventArgs e)
        {
            this.Validation.Text = string.Empty;
            Profile user = Profile.AuthenticateSql(this.txtUserName.Text, this.txtPassword.Text);
            if (user != null)
            {
                user.LogOn(true);
                this.Validation.Text = "<li>Login Successful.</li>";
                this.LogOnForm.Visible = false;
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    Response.Redirect(HttpUtility.UrlDecode(Request.QueryString["ReturnUrl"]));
                }
            }
            else
            {
                this.Validation.Text = "<li>Login Failure: Unknown user name or bad password.</li>";
            }
        }

        /// <summary>
        /// This method is triggered by clicking the Logout button.
        /// </summary>
        /// <param name="sender">The caller of the event.</param>
        /// <param name="e">The parameters of the event.</param>
        protected void LogOff_Click(object sender, EventArgs e)
        {
            Profile.Current.LogOff();
            this.LogOnForm.Visible = true;
            this.LogOffForm.Visible = false;
            this.Validation.Text = "<li>Logout Successful.</li>";
        }
    }
}